function [y,N,D]=neighbors_y(train_x,train_y,test_x)

%NEIGHBORS_Y assigns to test values the outcome of that of the closest neighbour in the training set
%Syntax: [y,N,D]=neighbors_y(train_x,train_y,test_x)
%Description: each column documents a variable and each row an event / sample 
%             y is the predicted outcome and N is the index of the closest
%             neighbor, where that outcome took place.
%
%
%Jonas Almeida, 16 Feb 2006

n=length(test_x(:,1));
m=length(train_x(:,1));N=zeros(n,1);D=N;
for i=1:n
    [D(i),N(i)]=min(sum((train_x-repmat(test_x(i,:),m,1)).^2,2)); %Euclidean distances between ith test x and all training xs
end
y=train_y(N,:);